import java.util.Vector;


public class MySpaceParser {

	private Vector<Xtag> tags=new Vector<Xtag>();
	private String Data;
	private String bandName;
	
	public MySpaceParser(String bandName, String Data)
	{
		this.bandName=bandName;
		this.Data=Data;
	}
	
	public void parse()
	{
		Xtag tag;
		String str;
		int startIndex;
		tags.removeAllElements();
		
		str=Data.trim();
		while (str.length()>0)
		{
			tag=new Xtag(str, 0);
			startIndex=tag.parseTag();
			tags.add(tag);
			
			str=str.substring(startIndex).trim();		}
	}
	
	public void parseProfileDiv()
	{
		Xtag tag;
		String str;
		int startIndex;
		tags.removeAllElements();
		
		filterBandEventsDiv();
		
		str=Data.trim();
		while (str.length()>0)
		{
			tag=new Xtag(str, 0);
			startIndex=tag.parseTag();
			tags.add(tag);
			
			str=str.substring(startIndex).trim();		}
	}
	
	public Manifestazioni getEventsListFiltered()
	{
		int i;
		Xtag bandProfileTag, table, tr;
		Manifestazioni eventi = new Manifestazioni();

		if (tags.size()==0)
			return eventi;
			
		bandProfileTag=tags.get(0);
		
		table = bandProfileTag.getSubTag(0).getSubTag(1).getSubTag(0).getSubTag(0);
		i=0;
		Evento evento;
		while((tr=table.getSubTag(i))!=null && (tr.getName().equals("tr")))
		{
			
			evento=Xtag.parseRow(tr);
			if (evento!=null)
			{
				evento.setGru(bandName);
				eventi.addEvento(evento);
			}
			++i;
		}
		return eventi;
	}

	
	public Manifestazioni getEventsList()
	{
		int i;
		Xtag htmlTag, bandProfileTag, table, tr;
		Manifestazioni eventi = new Manifestazioni();
		htmlTag=null;
		for (i=0; i<tags.size(); i++)
		{
			htmlTag = tags.get(i);
			if(htmlTag.getName().equals("html"))
			{
				break;
			}
		}
		if (htmlTag==null)
		{
			return null;
		}
		bandProfileTag=htmlTag.getTagByID("profile_bandschedule");
		
		table = bandProfileTag.getSubTag(0).getSubTag(1).getSubTag(0).getSubTag(0);
		i=0;
		Evento evento;
		while((tr=table.getSubTag(i))!=null && (tr.getName().equals("tr")))
		{
			
			evento=Xtag.parseRow(tr);
			if (evento!=null)
				eventi.addEvento(evento);
			++i;
		}

		
		return eventi;
	}
	
	public boolean filterBandEventsDiv()
	{
		String strBandDivTag="<div id=\"profile_bandschedule\">";
		int startIndex, endIndex;
		startIndex=Data.indexOf(strBandDivTag);
		
		if(startIndex==-1)
		{
			Data="";
			return false;
		}
		endIndex=Data.indexOf("</div>", startIndex);
		if(endIndex==-1)
			return false;
		Data=Data.substring(startIndex, endIndex+"</div>".length());
		
		return true;
	}
	
}


